﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

namespace Pong_Project.Views
{
    class ChoosePowerupsSettings : View
    {
        private string header, back, allPowerups, explanation1, explanation2, biggerBat, increaseBatSpeed, reverseBatKeys, decreaseBatSpeed, smallerBat, ballTurnsFromYou, ballTurnAgainstYou, ballGoldenBallPowerup;
        private string biggerBatFact, increaseBatSpeedFact, reverseBatKeysFact, decreaseBatSpeedFact, smallerBatFact, ballTurnsFromYouFact, ballTurnAgainstYouFact, ballGoldenBallPowerupFact;
        private SpriteFont headerFont, textFont, poweupListText;
        private Vector2 headerPos, backPos, explanation1Pos, explanation2Pos, allPowerupsPos;
        private Vector2 headerOrigin, backOrigin, explanation1Origin, explanation2Origin;
        private Texture2D unselected, selected, biggerBatSprite, increaseBatSpeedSprite, reverseBatKeysSprite, decreaseBatSpeedSprite, smallerBatSprite, ballTurnsFromYouSprite, ballTurnAgainstYouSprite, ballGoldenBallPowerupSprite;

        private static int numberOfPowerups = 8;
        private List<bool> chosen = new List<bool>();
        private List<Vector2> powerupListPos2 = new List<Vector2>();
        private List<Vector2> powerupSpriteListPos = new List<Vector2>();
        private List<Vector2> powerupFactListPos = new List<Vector2>();

        private int selectedEntry = 1;
        private int minEntry = 1;
        private static int maxEntry = numberOfPowerups + 2;
        private bool selectedAllPowerups = false;


        public ChoosePowerupsSettings()
            : base()
        {
            header = "Choose Powerups";
            headerPos = new Vector2(Game.WindowWidth / 2, 35);

            explanation1 = "Press Enter to choose Powerup";
            explanation2 = "The pictures of powerups the ball can pick up is larger than the ones for the bat";
            explanation1Pos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 5 + 25 * numberOfPowerups + 20);
            explanation2Pos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 5 + 25 * numberOfPowerups + 40);

            back = "Back";
            backPos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 5 + 25 * numberOfPowerups + 60);

            allPowerups = "All powerups";
            allPowerupsPos = new Vector2(Game.WindowWidth / 5, Game.WindowHeight / 5 - 25);

            biggerBat = "Supersize Bat";
            biggerBatFact = "The bat becomes a super sized bat!";

            ballTurnsFromYou = "Bye bye ball";
            ballTurnsFromYouFact = "The ball is thrown from you!";

            increaseBatSpeed = "Super bat speed";
            increaseBatSpeedFact = "The bat gains superspeed!";

            decreaseBatSpeed = "Lazy bat";
            decreaseBatSpeedFact = "The bat is moving depressingly slow";

            reverseBatKeys = "When up is down";
            reverseBatKeysFact = "The bat is confused and the keys is reversed";

            smallerBat = "Cute little bat";
            smallerBatFact = "The bat shrinks and become small";

            ballTurnAgainstYou = "Evil ball";
            ballTurnAgainstYouFact = "The ball changes its mind and turn against you!";

            ballGoldenBallPowerup = "GOLD!";
            ballGoldenBallPowerupFact = "The bat gains a free score, hurray!";


            for (int i = 0; i < numberOfPowerups; i++)
            {
                chosen.Add(false);
            }

            //////////////////////////////////
            //Default powerups added
            ////////////////////////////////

            chosen[0] = true;
            chosen[1] = true;

            /////////////////////////////////
            GameObjectManager.ChosenPowerups(chosen);

            for (int i = 0; i < numberOfPowerups; i++)
            {
                powerupListPos2.Add(new Vector2(Game.WindowWidth / 5, Game.WindowHeight / 5 + 25 * i));
            }

            for (int i = 0; i < numberOfPowerups; i++)
            {
                powerupSpriteListPos.Add(new Vector2(powerupListPos2[i].X * 2, powerupListPos2[i].Y + 2));
                powerupFactListPos.Add(new Vector2(powerupListPos2[i].X * 2 + 25, powerupListPos2[i].Y));
            }
        }

        public override void LoadContent(ContentManager content)
        {
            headerFont = content.Load<SpriteFont>("Text");
            textFont = content.Load<SpriteFont>("Text");
            poweupListText = content.Load<SpriteFont>("PowerupList");

            selected = content.Load<Texture2D>("Selected");
            unselected = content.Load<Texture2D>("Unselected");

            headerOrigin = headerFont.MeasureString(header) / 2;
            backOrigin = headerFont.MeasureString(back) / 2;
            explanation1Origin = textFont.MeasureString(explanation1) / 2;
            explanation2Origin = textFont.MeasureString(explanation2) / 2;

            biggerBatSprite = content.Load<Texture2D>("PU_BiggerBat");
            increaseBatSpeedSprite = content.Load<Texture2D>("PU_IncreaseBatSpeed");
            reverseBatKeysSprite = content.Load<Texture2D>("PU_ReverseBatKeys");
            ballTurnsFromYouSprite = content.Load<Texture2D>("PU_BallTurnsFromYou");
            decreaseBatSpeedSprite = content.Load<Texture2D>("PU_DecreaseBatSpeed");
            smallerBatSprite = content.Load<Texture2D>("PU_SmallerBat");
            ballTurnAgainstYouSprite = content.Load<Texture2D>("PU_BallTurnsAgainstYou");
            ballGoldenBallPowerupSprite = content.Load<Texture2D>("PU_GoldenBallPowerup");
        }

        public override void Update(GameTime gameTime)
        {
            if (Controls.MuteSound)
            {
                MediaPlayer.IsMuted = !MediaPlayer.IsMuted;
            }

            if (Controls.MenuDown)
            {
                if (selectedEntry + 1 > maxEntry)
                    selectedEntry = minEntry;
                else
                    selectedEntry++;
            }

            else if (Controls.MenuUp)
            {
                if (selectedEntry - 1 < minEntry)
                    selectedEntry = maxEntry;
                else
                    selectedEntry--;
            }

            if (Controls.Enter)
            {
                switch (selectedEntry)
                {
                    case 1://select all powerups
                        if (selectedAllPowerups == false)
                        {
                            for (int i = 0; i < chosen.Count; i++)
                                chosen[i] = true;
                            selectedAllPowerups = true;
                        }
                        else
                        {
                            for (int i = 0; i < chosen.Count; i++)
                                chosen[i] = false;
                            selectedAllPowerups = false;
                        }
                        break;

                    case 2: // biggerBat
                        if (chosen[0] == true)
                        {
                            chosen[0] = false;
                            selectedAllPowerups = false;
                        }
                        else chosen[0] = true;
                        break;

                    case 3: //oppositeBallDirection
                        if (chosen[1] == true)
                        {
                            chosen[1] = false;
                            selectedAllPowerups = false;
                        }
                        else chosen[1] = true;
                        break;

                    case 4: //increaseBatSpeed
                        if (chosen[2] == true)
                        {
                            chosen[2] = false;
                            selectedAllPowerups = false;
                        }
                        else chosen[2] = true;
                        break;

                    case 5: //decreaseBatSpeed
                        if (chosen[3] == true)
                        {
                            chosen[3] = false;
                            selectedAllPowerups = false;
                        }
                        else chosen[3] = true;
                        break;

                    case 6: //reverseBatKeys
                        if (chosen[4] == true)
                        {
                            chosen[4] = false;
                            selectedAllPowerups = false;
                        }
                        else chosen[4] = true;
                        break;

                    case 7:  // smallerBat
                        if (chosen[5] == true)
                        {
                            chosen[5] = false;
                            selectedAllPowerups = false;
                        }
                        else chosen[5] = true;
                        break;

                    case 8: // ballTurnAganstYou
                        if (chosen[6] == true)
                        {
                            chosen[6] = false;
                            selectedAllPowerups = false;
                        }
                        else chosen[6] = true;
                        break;

                    case 9: //ballGoldenBallPowerup
                        if (chosen[7] == true)
                        {
                            chosen[7] = false;
                            selectedAllPowerups = false;
                        }
                        else chosen[7] = true;
                        break;

                    case 10: // back
                        ViewManager.State = GameState.Settings;
                        GameObjectManager.ChosenPowerups(chosen);
                        break;

                    default:
                        throw new ArgumentException("Invalid menu entry: " + selectedEntry);
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(headerFont, header, headerPos, Color.White, 0, headerOrigin, 1.0f, SpriteEffects.None, 0.5f);

            spriteBatch.DrawString(textFont, explanation1, explanation1Pos, Color.White, 0, explanation1Origin, 0.65f, SpriteEffects.None, 1);
            spriteBatch.DrawString(textFont, explanation2, explanation2Pos, Color.White, 0, explanation2Origin, 0.65f, SpriteEffects.None, 1);

            for (int i = 0; i < chosen.Count; i++)
            {
                if (chosen[i] == false)
                {
                    spriteBatch.Draw(unselected, new Vector2(powerupListPos2[i].X - 30, powerupListPos2[i].Y + 5), Color.White);
                }
                else
                    spriteBatch.Draw(selected, new Vector2(powerupListPos2[i].X - 30, powerupListPos2[i].Y + 5), Color.White);
            }

            //Selected all powerups
            if (selectedEntry != 1)
                spriteBatch.DrawString(headerFont, allPowerups, allPowerupsPos, Color.White, 0, Vector2.Zero, 0.9f, SpriteEffects.None, 0.5f);
            else
                spriteBatch.DrawString(headerFont, allPowerups, allPowerupsPos, Color.Red, 0, Vector2.Zero, 0.9f, SpriteEffects.None, 0.5f);
            if (selectedAllPowerups == false)
                spriteBatch.Draw(unselected, new Vector2(powerupListPos2[0].X - 30, powerupListPos2[0].Y - 20), Color.White);
            else
                spriteBatch.Draw(selected, new Vector2(powerupListPos2[0].X - 30, powerupListPos2[0].Y - 20), Color.White);

            //biggerBat
            if (selectedEntry != 2)
                spriteBatch.DrawString(poweupListText, biggerBat, powerupListPos2[0], Color.White);
            else
                spriteBatch.DrawString(poweupListText, biggerBat, powerupListPos2[0], Color.Red);
            spriteBatch.DrawString(poweupListText, biggerBatFact, powerupFactListPos[0], Color.White);
            spriteBatch.Draw(biggerBatSprite, powerupSpriteListPos[0], null, Color.White, 0, Vector2.Zero, 0.6f, SpriteEffects.None, 1);

            //ballTurnAgainstYou
            if (selectedEntry != 3)
                spriteBatch.DrawString(poweupListText, ballTurnsFromYou, powerupListPos2[1], Color.White);
            else
                spriteBatch.DrawString(poweupListText, ballTurnsFromYou, powerupListPos2[1], Color.Red);
            spriteBatch.DrawString(poweupListText, ballTurnsFromYouFact, powerupFactListPos[1], Color.White);
            spriteBatch.Draw(ballTurnsFromYouSprite, powerupSpriteListPos[1], Color.White);

            //increaseBatSpeed
            if (selectedEntry != 4)
                spriteBatch.DrawString(poweupListText, increaseBatSpeed, powerupListPos2[2], Color.White);
            else
                spriteBatch.DrawString(poweupListText, increaseBatSpeed, powerupListPos2[2], Color.Red);
            spriteBatch.DrawString(poweupListText, increaseBatSpeedFact, powerupFactListPos[2], Color.White);
            spriteBatch.Draw(increaseBatSpeedSprite, powerupSpriteListPos[2], null, Color.White, 0, Vector2.Zero, 0.6f, SpriteEffects.None, 1);

            //decreaseBatSpeed
            if (selectedEntry != 5)
                spriteBatch.DrawString(poweupListText, decreaseBatSpeed, powerupListPos2[3], Color.White);
            else
                spriteBatch.DrawString(poweupListText, decreaseBatSpeed, powerupListPos2[3], Color.Red);
            spriteBatch.DrawString(poweupListText, decreaseBatSpeedFact, powerupFactListPos[3], Color.White);
            spriteBatch.Draw(decreaseBatSpeedSprite, powerupSpriteListPos[3], null, Color.White, 0, Vector2.Zero, 0.6f, SpriteEffects.None, 1);


            //reverseBatKeys
            if (selectedEntry != 6)
                spriteBatch.DrawString(poweupListText, reverseBatKeys, powerupListPos2[4], Color.White);
            else
                spriteBatch.DrawString(poweupListText, reverseBatKeys, powerupListPos2[4], Color.Red);
            spriteBatch.DrawString(poweupListText, reverseBatKeysFact, powerupFactListPos[4], Color.White);
            spriteBatch.Draw(reverseBatKeysSprite, powerupSpriteListPos[4], null, Color.White, 0, Vector2.Zero, 0.6f, SpriteEffects.None, 1);


            //smallerBat
            if (selectedEntry != 7)
                spriteBatch.DrawString(poweupListText, smallerBat, powerupListPos2[5], Color.White);
            else
                spriteBatch.DrawString(poweupListText, smallerBat, powerupListPos2[5], Color.Red);
            spriteBatch.DrawString(poweupListText, smallerBatFact, powerupFactListPos[5], Color.White);
            spriteBatch.Draw(smallerBatSprite, powerupSpriteListPos[5], null, Color.White, 0, Vector2.Zero, 0.6f, SpriteEffects.None, 1);

            //ballTurnsAgainstYou
            if (selectedEntry != 8)
                spriteBatch.DrawString(poweupListText, ballTurnAgainstYou, powerupListPos2[6], Color.White);
            else
                spriteBatch.DrawString(poweupListText, ballTurnAgainstYou, powerupListPos2[6], Color.Red);
            spriteBatch.DrawString(poweupListText, ballTurnAgainstYouFact, powerupFactListPos[6], Color.White);
            spriteBatch.Draw(ballTurnAgainstYouSprite, powerupSpriteListPos[6], Color.White);

            //ballGoldenBallPowerup
            if (selectedEntry != 9)
                spriteBatch.DrawString(poweupListText, ballGoldenBallPowerup, powerupListPos2[7], Color.White);
            else
                spriteBatch.DrawString(poweupListText, ballGoldenBallPowerup, powerupListPos2[7], Color.Red);
            spriteBatch.DrawString(poweupListText, ballGoldenBallPowerupFact, powerupFactListPos[7], Color.White);
            spriteBatch.Draw(ballGoldenBallPowerupSprite, powerupSpriteListPos[7], null, Color.White, 0, Vector2.Zero, 0.6f, SpriteEffects.None, 1);

            if (selectedEntry != 10)
                spriteBatch.DrawString(textFont, "Back", backPos, Color.White, 0, backOrigin, 1.0f, SpriteEffects.None, 0.5f);
            else
                spriteBatch.DrawString(textFont, back, backPos, Color.Red, 0, backOrigin, 1.0f, SpriteEffects.None, 0.5f);

        }
    }
}
